
/**
 * 16进制-->2进制  hexTobinary.js
 */
//我们需要变成0000000000101001 好的测试代码用tape，烂的测试代码如下
const hex="0x29";//test数据
const binary = parseInt(hex, 16).toString(2);
console.log(binary); //101001
const oldLength = binary.length;
console.log(oldLength);//6
const hexLen =hex.toString().length*4 //长度要16
//oldLength < hex.toString().length * 4 写个for迭代器在拼接保留下

//不用第三方库的处理的代码
function hex2binary(hex) {

    var binary = parseInt(hex, 16).toString(2);
    //16进制长度
    var oldLength = binary.length;
    //16进制长度*4等于2进制
    if (oldLength < hex.toString().length * 4) {
        for (var i = 0; i < hex.toString().length * 4 - oldLength; i++)
            //保留拼接保留0
            binary = '0' + binary;
    }
    return binary;
}
var hex="0x29";
console.log(hex2binary(hex)); //cls